home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / rules / stubs / stubtuple.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  4.4 KB  |  173 lines

  1.  
  2. /*===================================================================
  3.  *
  4.  * FILE:
  5.  * stubtuple.c
  6.  *
  7.  * IDENTIFICATION:
  8.  * $Header: /private/postgres/src/rules/stubs/RCS/stubtuple.c,v 1.12 1992/08/21 05:48:57 mer Exp $
  9.  *
  10.  *
  11.  *===================================================================
  12.  */
  13.  
  14.  
  15. #include <stdio.h>
  16. #include "tmp/postgres.h"
  17.  
  18. #include "utils/log.h"
  19. #include "access/tupdesc.h"
  20. #include "access/heapam.h"
  21. #include "utils/fmgr.h"
  22. #include "rules/prs2.h"
  23. #include "rules/prs2stub.h"
  24. #include "parser/parse.h"       /* for the AND, NOT, OR */
  25. #include "nodes/plannodes.h"
  26. #include "nodes/execnodes.h"
  27. #include "nodes/execnodes.a.h"
  28. #include "nodes/primnodes.h"
  29. #include "nodes/primnodes.a.h"
  30. #include "executor/x_tuples.h"
  31. #include "executor/tuptable.h"
  32.  
  33. extern ExprContext RMakeExprContext();
  34.  
  35. /*--------------------------------------------------------------------
  36.  *
  37.  * prs2StubGetLocksForTuple
  38.  *
  39.  * given a collection of stub records and a tuple, find all the locks
  40.  * that the tuple must inherit.
  41.  *
  42.  *--------------------------------------------------------------------
  43.  */
  44. RuleLock
  45. prs2StubGetLocksForTuple(tuple, buffer, tupDesc, stubs)
  46. HeapTuple tuple;
  47. Buffer buffer;
  48. TupleDescriptor tupDesc;
  49. Prs2Stub stubs;
  50. {
  51.     int i;
  52.     RuleLock temp, resultLock;
  53.     Prs2OneStub oneStub;
  54.  
  55.     /*
  56.      * resultLock is initially an empty lock
  57.      */
  58.     resultLock = prs2MakeLocks();
  59.  
  60.     for (i=0; i<stubs->numOfStubs; i++) {
  61.     oneStub = stubs->stubRecords[i];
  62.     if (prs2StubQualTestTuple(tuple, buffer, tupDesc,
  63.                 oneStub->qualification)){
  64.         temp = prs2LockUnion(resultLock, oneStub->lock);
  65.         prs2FreeLocks(resultLock);
  66.         resultLock = temp;
  67.     }
  68.     }
  69.  
  70.     return(resultLock);
  71.  
  72. }
  73.  
  74. /*--------------------------------------------------------------------
  75.  *
  76.  * prs2StubQualTestTuple
  77.  *
  78.  * test if a tuple satisfies the given qualifications of a
  79.  * stub record.
  80.  *--------------------------------------------------------------------
  81.  */
  82. bool
  83. prs2StubQualTestTuple(tuple, buffer, tupDesc, qual)
  84. HeapTuple tuple;
  85. Buffer buffer;
  86. TupleDescriptor tupDesc;
  87. LispValue qual;
  88. {
  89.     ExprContext econtext;
  90.     bool qualResult;
  91.     TupleTable tupleTable;
  92.     TupleTableSlot slot;
  93.     int slotnum;
  94.  
  95.     /*
  96.      * Use ExecEvalQual to test for the qualification.
  97.      * To do that create a dummy 'ExprContext' & fill it
  98.      * with the appropriate info.
  99.      *
  100.      * Note also, that as 'ExecQual' expects its tuple
  101.      * to be in a 'tuple table slot' we have to create
  102.      * a dummy one.
  103.      * It would be nice to keep it for a long time and not
  104.      * allocate it & deallocate it continuously,
  105.      * but we can not keep it as a static variable because the
  106.      * memory is pfreed by the end of the Xact.
  107.      * (No, I didn't think all that by just reading the code,
  108.      * I tried it & learned the hard way... sigh!)
  109.      */
  110.  
  111.     /*
  112.      * Initialize the tuple table stuff.
  113.      */
  114.     tupleTable = ExecCreateTupleTable(1);
  115.     slotnum =      ExecAllocTableSlot(tupleTable);
  116.     slot = (TupleTableSlot)
  117.     ExecGetTableSlot(tupleTable, slotnum);
  118.     
  119.     (void) ExecStoreTuple((Pointer)tuple, (Pointer)slot, buffer, false);
  120.     (void) ExecSetSlotDescriptor(slot, tupDesc);
  121.     (void) ExecSetSlotBuffer(slot, buffer);
  122.     
  123.     /*
  124.      *    Initialize expression context.
  125.      */
  126.     econtext = RMakeExprContext();
  127.     set_ecxt_scantuple(econtext, slot);
  128.     set_ecxt_innertuple(econtext, NULL);
  129.     set_ecxt_outertuple(econtext, NULL);
  130.     set_ecxt_param_list_info(econtext, NULL);
  131.     set_ecxt_range_table(econtext, NULL);
  132.  
  133.     /*
  134.      * NOTE: we must reinitialize the fcache of all Oper nodes to NULL
  135.      * changes the 'fcache' attribute of the operator nodes in it.
  136.      * So if this is a "shared" qual (for instance if it belongs to
  137.      * a "relation level" stub cached in the EState) then
  138.      * the first time we call ExecQual it will initialize the fcache
  139.      * then it will free the memory, and so when we try to call ExecQual
  140.      * for the second time it will see garbage and die a horrible death.
  141.      */
  142.     prs2ReInitQual(qual);
  143.     qualResult = ExecQual(qual, econtext);
  144.  
  145.     /*
  146.      * release the tuple table stufffffff....
  147.      */
  148.     ExecDestroyTupleTable(tupleTable, false);
  149.  
  150.     if (qualResult) {
  151.     return(true);
  152.     } else {
  153.     return(false);
  154.     }
  155. }
  156.  
  157. prs2ReInitQual(list)
  158. LispValue list;
  159. {
  160.     LispValue t;
  161.  
  162.     if (null(list))
  163.     return;
  164.  
  165.     if (IsA(list,Oper)) {
  166.     set_op_fcache((Oper)list, NULL);
  167.     return;
  168.     }
  169.     if (consp(list))
  170.     foreach(t, list)
  171.         prs2ReInitQual(CAR(t));
  172. }
  173.